package javaApi.HDFSClientShell;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.util.Scanner;

public class ClientHDFS {
    Configuration conf;
    FileSystem fs;

    public ClientHDFS() throws IOException {
        conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop-master:9000");
        fs = FileSystem.get(conf);
    }

    public void makeDirectory(String dirName) throws IOException {
        fs.mkdirs(new Path("./ZYSoft/" + dirName));
        System.out.println("The Directory is made successfully");

    }

    public void makeFile(String filName) throws IOException {
        fs.mkdirs(new Path("./ZYSoft/" + filName + ".txt"));
        System.out.println("The file is created !");
    }

    public void deleteDirectory(String dreName) throws IOException {
        fs.delete(new Path("./ZYSoft/" + dreName), true);
        System.out.println("The Directory is deleted successfully");
    }

    public void renameDirectory(String oldDirectoryName, String newDirectoryName) throws IOException {

        fs.rename(new Path("./ZYSoft/" + oldDirectoryName), new Path("./ZYSoft/" + newDirectoryName));
        System.out.println("Successfully rename");
    }

    public static void main(String[] args) throws IOException {
        String choice;
        Scanner sc = new Scanner(System.in);
        ClientHDFS c1 = new ClientHDFS();
        do {
            System.out.println("**************************Menu************************");
            System.out.println("1.Create a new Directory                             *");
            System.out.println("2.Create a new File                                  *");
            System.out.println("3.Delete a Directory                                 *");
            System.out.println("4.Rename a new Directory                             *");
            System.out.println("5.Copy from Local Windows File System to HDFS        *");
            System.out.println("******************************************************");
            System.out.println("Please Enter your choice:");
            String op = sc.next();
            switch (op) {
                case "1":
                    System.out.println("Please Enter DirectoryName:");
                    String dirName = sc.next();
                    c1.makeDirectory(dirName);
                    break;
                case "2":
                    System.out.println("Enter Filename:");
                    String filename = sc.next();
                    c1.makeFile(filename);
                    break;

                case "3":
                    System.out.println("Enter Directoryname you want to delete:");
                    String drename = sc.next();
                    c1.deleteDirectory(drename);
                    break;
                case "4":
                    System.out.println("Enter oldDirectoryname:");
                    String olddirname = sc.next();
                    System.out.println("Enter newDirectoryname:");
                    String newdirname = sc.next();
                    c1.renameDirectory(olddirname, newdirname);
                    break;
                default:
                    System.out.println("Please enter valid number");
                    break;

            }
            System.out.println("Do you want to continue ?");
            choice = sc.next();


        } while (choice.equals("yes") || choice.equals("YES"));
        c1.fs.close();

    }

}
